if(typeof(Todo) == 'undefined'){
	Todo = {};
}

IEBROWSER = false;
FIREFOX = false;

if(typeof String.prototype.trim!=='function'){
	String.prototype.trim=function(){
		return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/,"$1");
	};
};

browserCheck = function(){
	var ua = navigator.userAgent;
	OPERABROWSER = navigator.appName === "Opera";
	MOZILLABROWSER = navigator.appName === "Netscape";
	FIREFOXBROWSER = ua.indexOf("Firefox/") !== -1;
	IEBROWSER = navigator.appName === "Microsoft Internet Explorer";
	CHROMEBROWSER = ua.indexOf("Chrome/") !== -1;
};

Todo.checkStorage =  function(){
	browserCheck();
	if(IEBROWSER){
		localStorage = window.localStorage;
	}
	if(!localStorage){ 
		var table = document.getElementById("tasklist");
		table.style.display ="none";
		alert("Local Storage is not supported by your browser.");
	}
};

Todo.setValue = function(formId){
	var form = document.getElementById(formId);
	var value = form["todotask"].value;
	var prior = form["priority"].value;
	var dueDate = form["date"].value;
	var date = new Date();
	var key = "ToDo"+date.getTime();
	value = value.trim();
	if(!value) {
		alert("Please enter a valid task.");
		form["todotask"].focus();
		return false;
	}
	if(!prior){
		alert("Please select the priority.");
		form["priority"].focus();
		return false;
	}
	value = value.replace(/&/g,'&amp;');
	value = value.replace(/</g,"&lt;");
	value = value.replace(/>/g,"&gt;");
	value = value+"::"+ prior+"::"+dueDate;
	//console.log(" "+value + prior+" "+key);
	localStorage.setItem(key,value);
	Todo.retreiveAll('tasklist');
	return true;
};

Todo.retreiveAll = function(div){
	//console.log("retreive all ");
	var elem = document.getElementById(div);var table = elem;
	elem = elem.children[1];
	//console.log("ell "+elem);
	var tasks = "";
	if(localStorage.length>0){
		var nodata = document.getElementById('nodata');
		nodata.style.display = 'none';
		table.style.display = "";
		for(var i=0;i<localStorage.length;i++){
			key = localStorage.key(i);
			if(key.indexOf("ToDo")!==-1){
				var total =  localStorage.getItem(key);
				total = total.split("::");
				tr ="<tr><td><input type='checkbox' title='Delete this task' name='"+key+"' value='"+key+
						"'></td>";
				for(var j=0;j<total.length;j++){
					var style ="";
					if(j==1 && total[j]==='Critical') style ="style='color:red;'" ;
					if(total[j] === undefined || total[j] ==='') total[j] = "Not Available";
					tr = tr+"<td "+style+" >"+total[j]+"</td>";
				}
				tasks = tasks+tr+"</tr>";
			}
		}
		elem.innerHTML = tasks;
		jQuery('td input:checkbox').bind("click",function(){
			if(confirm("Is this task done?")){
				Todo.deleteItem(this.value,this.parentNode);
			}else{
				this.checked =false;
			}
		});
	}else{
		Todo.makeVisible();
	}
};

Todo.deleteItem = function(key,td){
	//console.log("Deleted Item "+td);
	try{
		localStorage.removeItem(key);
		var tr = td.parentNode;
		tr.parentNode.removeChild(tr);
		Todo.makeVisible();
	}catch(e){
		
	}
};

Todo.makeVisible = function(){
	var table = document.getElementById("tasklist");
	var nodata = document.getElementById('nodata');
	if(localStorage.length <= 0){
		nodata.style.display = '';
		table.style.display = "none";
	}else{
		nodata.style.display = 'none';
		table.style.display = '';
	}
};

Todo.clear = function(){
	localStorage.clear();
	Todo.makeVisible();
};

Todo.confirmClear = function(){
	if(localStorage.length <= 0){
		alert("There is no task to delete!!!");
		return;
	}
	var t = confirm("Do you want to delete all tasks?");
	if(t){
		Todo.clear();
	}
	
};

jQuery(document).ready(function(){
	Todo.checkStorage();
	Todo.retreiveAll('tasklist');
	jQuery('span.check').mousedown(function(){
		this.style.backgroundColor='rgb(200,200,200)';
	}).mouseup(function(){
		this.style.backgroundColor='whitesmoke';
	}).mouseout(function(){
		this.style.backgroundColor='whitesmoke';
	}).click(function(){
		Todo.retreiveAll('tasklist');
	});
	jQuery('span.deleteall').mousedown(function(){
		this.style.backgroundColor='rgb(200,200,200)';
	}).mouseup(function(){
		this.style.backgroundColor='whitesmoke';
	}).mouseout(function(){
		this.style.backgroundColor='whitesmoke';
	}).click(function(){
		Todo.confirmClear();
	});
	jQuery("#task").submit(function(){
		return Todo.setValue(this.id);
	});
});